#define _CRT_SECURE_NO_WARNINGS 1
#include "String.h"
namespace wang
{
	//定义npos - 由于这个是const静态成员变量,所以
	// 这里定义的时候初始化也可以,在声明的时候给缺省值也可以
	//const size_t String::npos = -1;
	const size_t String::npos = -1;
	using iterator = char*;
	using const_iterator = const char*;
	//无参构造
	/*String::String()
		:_str(new char[1]{ '\0' })
		, _size(0)
		, _capacity(0)
	{}*/
	//带参的构造
	String::String(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_size + 1];//+1是给'\0'预留的空间
		strcpy(_str, str);
	}
	//这种太短的函数其实可以放在类里面成为内联
	const char* String::c_str() const
	{
		return _str;
	}
	//拷贝构造 - 传统写法
	String::String(const String& s)
	{
		_str = new char[s._capacity + 1];
		for (int i = 0; i <= s._size; i++)
		{
			*(_str + i) = *(s._str + i);
		}
		//strcpy(_str, s._str);
		_capacity = s._capacity;
		_size = s._size;
	}
	//拷贝构造 - 现代写法 s1(s2)
	//String::String(const String& s)
	//{
	//	String tmp(s._str);//借助构造完成
	//	swap(tmp);
	//}

	//析构函数
	String::~String()
	{
		delete[] _str;
		_str = nullptr;
		_size = 0;
		_capacity = 0;
	}
	size_t String::size() const
	{
		return _size;
	}
	char& String::operator[](size_t i)
	{
		assert(i < _size);
		return _str[i];
	}
	const char& String::operator[](size_t i) const
	{
		assert(i < _size);
		return _str[i];
	}
	iterator String::begin()
	{
		//begin返回的是起始位置
		return _str;
	}
	iterator String::end()
	{
		//end返回的是最后一个位置
		return _str + _size;
	}
	//const迭代器
	const_iterator String::begin() const
	{
		return _str;
	}
	const_iterator String::end() const
	{
		return _str + _size;
	}
	void String::reserve(size_t n)
	{
		//cout << "reserve:" << n << endl;
		if (n > _capacity)
		{
			//+1是给'\0'预留的位置
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}
	void String::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		*(_str + _size + 1) = *(_str + _size);
		*(_str + _size) = ch;
		_size++;
		//insert(_size, ch);
	}
	void String::append(const char* str)
	{
		int len = strlen(str);
		if (_size + len > _capacity)
		{
			int newCapacity = 2 * _capacity;
			if (newCapacity < _size + len)
				newCapacity = _size + len;
			reserve(newCapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}
	String& String::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}
	String& String::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	void String::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		/*for (int i = _size; i >= (int)pos; i--)
		{
			*(_str + i + 1) = *(_str + i);
		}*/
		for (size_t i = _size + 1; i > pos; i--)
		{
			*(_str + i) = *(_str + i - 1);
		}
		*(_str + pos) = ch;
		_size++;
	}
	void String::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		int len = strlen(str);
		if (_size + len > _capacity)
		{
			int newCapacity = 2 * _capacity;
			if (newCapacity < _size + len)
				newCapacity = _size + len;
			reserve(newCapacity);
		}
		/*for (int i = _size; i >= (int)pos; i--)
		{
			*(_str + i + len) = *(_str + i);
		}*/
		for (int i = _size + len; i > pos + len - 1; i--)
		{
			*(_str + i) = *(_str + i - len);
		}
		for (int i = 0; i < len; i++)
		{
			*(_str + pos + i) = *(str + i);
		}
		_size += len;
	}
	void String::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (pos + len >= _size)
		{
			*(_str + pos) = '\0';
			_size = pos;
		}
		else
		{
			for(int i = pos + len;i <= _size;i++)
			{
				*(_str + i - len) = *(_str + i);
			}
			_size -= len;
		}
	}
	size_t String::find(char ch, size_t pos)
	{
		assert(pos < _size);
		for (int i = pos; i < _size; i++)
		{
			if (*(_str + i) == ch)
				return i;
		}
		return npos;
	}
	size_t String::find(const char* str, size_t pos)
	{
		assert(pos < _size);
		const char* ptr = strstr(_str + pos, str);
		if (ptr == nullptr)
		{
			return npos;
		}
		else
		{
			return ptr - _str;
		}
	}
	String String::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > (_size - pos))
		{
			len = _size - pos;
		}
		wang::String Substr;
		Substr.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			Substr += *(_str + pos + i);
		}
		//cout << Substr.c_str() << endl;
		//传值返回调用拷贝构造
		return Substr;
	}
	//赋值 - 传统写法
	//String& String::operator= (const String& s)
	//{
	//	//判断一下,避免自己给自己赋值
	//	if (this != &s)
	//	{
	//		delete[] _str;
	//		_str = new char[s._capacity + 1];
	//		for (int i = 0; i <= s._size; i++)
	//		{
	//			*(_str + i) = *(s._str + i);
	//		}
	//		_capacity = s._capacity;
	//		_size = s._size;
	//	}
	//	return *this;
	//}
	//赋值 - 现代写法
	//String& String::operator= (const String& s)
	//{
	//	//判断是不是自己给自己赋值
	//	if (this != &s)
	//	{
	//		String tmp(s._str);
	//		swap(tmp);
	//	}
	//	return *this;
	//}
	//赋值构造 - 现代写法2
	String& String::operator= (String s)
	{
		swap(s);
		return *this;
	}
	void String::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	void String::swap(String& s)
	{
		//会优先调用自己,但是参数不匹配,
		//就得指定命名空间域
		std::swap(_str, s._str);
		std::swap(_capacity, s._capacity);
		std::swap(_size, s._size);
	}
	//字符串str1比字符串str2大就返回1,相等返回0,小返回-1
	int StrCmp(const char* str1, const char* str2)
	{
		while (*str1 != '\0' && *str2 != '\0')
		{
			if (*str1 == *str2)
			{
				str1++;
				str2++;
			}
			else if (*str1 > *str2)
			{
				return 1;
			}
			else if (*str1 < *str2)
			{
				return -1;
			}
		}
		if (*str1 == '\0' && *str2 == '\0') return 0;
		if (*str1 == '\0') return -1;
		if (*str2 == '\0') return 1;
	}
	bool operator== (const String& lhs, const String& rhs)
	{
		return !(lhs != rhs);
	}
	bool operator!= (const String& lhs, const String& rhs)
	{
		return StrCmp(lhs.c_str(), rhs.c_str()) != 0;
	}
	bool operator> (const String& lhs, const String& rhs)
	{
		return StrCmp(lhs.c_str(), rhs.c_str()) > 0;
	}
	bool operator< (const String& lhs, const String& rhs)
	{
		return !(lhs == rhs || lhs > rhs);
	}
	bool operator>= (const String& lhs, const String& rhs)
	{
		return (lhs == rhs || lhs > rhs);
	}
	bool operator<= (const String& lhs, const String& rhs)
	{
		return !(lhs > rhs);
	}
	ostream& operator<< (ostream& out, const String& str)
	{
		//这里严格来说可以输出c_str(),但c_str()在极端情况下会出问题
		for (size_t i = 0; i < str.size(); i++)
		{
			out << str[i];
		}
		return out;
	}
	istream& operator>> (istream& in, String& str)
	{
		//清除str中的内容
		str.clear();
		int i = 0;
		//栈上开空间比堆上开空间效率高,栈上分配空间,编译的时候就算好了
		//这个函数结束,随着栈帧的销毁这个数组也就销毁了,即用即销毁,开大
		//一点无所谓,也不存在浪费。
		char buff[256];
		char ch;
		ch = in.get();
		//这里我们可以自定义结束标志
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 255)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			str += buff;
		}
		return in;
	}
	istream& getline(istream& in, String& str, char delim)
	{
		str.clear();
		int i = 0;
		char buff[256];
		char ch;
		ch = in.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == 255)
			{
				buff[i] = '\0';
				str += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			str += buff;
		}
		return in;
	}
	void swap(String& s1, String& s2)
	{
		s1.swap(s2);
	}
}
